package com.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.core.mapper.OperationLogMapper;
import com.core.vo.OperationLogVo;
import com.core.domain.OperationLogs;
import com.core.resp.PageResult;
import com.core.service.OperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import io.netty.util.internal.StringUtil;

import java.util.List;

@Slf4j
@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLogs> implements OperationLogService {
    @Override
    public boolean save(OperationLogs operationLog) {
        return super.save(operationLog);
    }

    @Override
    public PageResult<OperationLogVo> selectPage(Long pageSize, Long pageNum, OperationLogs query) {
        Long startIndex = (pageNum - 1) * pageSize;
        List<OperationLogVo> logs = baseMapper.selectPage(pageSize, startIndex, query);
        
        LambdaQueryWrapper<OperationLogs> queryWrapper = new LambdaQueryWrapper<>();
        if (query != null) {
            if (query.getUserId() != null) {
                queryWrapper.eq(OperationLogs::getUserId, query.getUserId());
            }
            if (!StringUtil.isNullOrEmpty(query.getOperationType())) {
                queryWrapper.eq(OperationLogs::getOperationType, query.getOperationType());
            }
            if (!StringUtil.isNullOrEmpty(query.getModule())) {
                queryWrapper.eq(OperationLogs::getModule, query.getModule());
            }
            if (!StringUtil.isNullOrEmpty(query.getDescription())) {
                queryWrapper.like(OperationLogs::getDescription, query.getDescription());
            }
        }
        
        Long total = baseMapper.selectCount(queryWrapper);
        return new PageResult<>(pageSize, pageNum, logs, total);
    }
} 